package com.ruoyi.develop.factory.factoryTestStandard.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.service.IGoodsOrderInService;


import com.ruoyi.generator.domain.GenTable;
import com.ruoyi.generator.domain.GenTableColumn;
import com.ruoyi.generator.service.IGenTableColumnService;
import com.ruoyi.generator.service.IGenTableService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.factory.factoryTestStandard.domain.FactoryTestStandard;
import com.ruoyi.develop.factory.factoryTestStandard.service.IFactoryTestStandardService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 检验标准Controller
 *
 * @author lmm
 * @date 2024-12-25
 */
@RestController
@RequestMapping("/factoryTestStandard/factoryTestStandard")
public class FactoryTestStandardController extends BaseController {
    @Autowired
    private IFactoryTestStandardService factoryTestStandardService;
    @Autowired
    private IGoodsOrderInService goodsOrderInService;
    @Autowired
    private IGenTableColumnService genTableColumnService;
    @Autowired
    private IGenTableService genTableService;

    /**
     * 查询检验标准列表
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestStandard:query')")
    @GetMapping("/list")
    public TableDataInfo list(FactoryTestStandard entity) {
        startPage();
        List<FactoryTestStandard> list = factoryTestStandardService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出检验标准列表
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestStandard:export')")
    @Log(title = "检验标准", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FactoryTestStandard entity) {
        List<FactoryTestStandard> list = factoryTestStandardService.selectList(entity);
        ExcelUtil<FactoryTestStandard> util = new ExcelUtil<FactoryTestStandard>(FactoryTestStandard.class);
        util.exportExcel(response, list, "检验标准数据");
    }

    /**
     * 获取检验标准详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestStandard:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(factoryTestStandardService.selectById(id));
    }

    /**
     * 新增检验标准
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestStandard:add')")
    @Log(title = "检验标准", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FactoryTestStandard entity) {
        return toAjax(factoryTestStandardService.insert(entity));
    }

    @PostMapping("/batchInsert")
    public AjaxResult batchInsert(@Validated @RequestBody FactoryTestStandard entity) {
        FactoryTestStandard param = new FactoryTestStandard();
        param.setGoodsId(entity.getGoodsId());
        param.setMaterialId(entity.getMaterialId());
        param.setTestType(entity.getTestType());
        List<FactoryTestStandard> standardList =  factoryTestStandardService.selectList(param);
        //判断该产品是否已经存在检验标准 如果存在则更新
        if(!standardList.isEmpty()){
            for (FactoryTestStandard standard : standardList) {
                factoryTestStandardService.deleteByIdPhysics(standard.getId());
            }
        }
        for (FactoryTestStandard standard : entity.getColumns()) {
            standard.setMaterialId(entity.getMaterialId());
            standard.setMaterialName(entity.getMaterialName());
            standard.setTestType(entity.getTestType());
            standard.setGoodsId(entity.getGoodsId());
            if (StringUtils.isNotBlank(standard.getGoodsId())) {
                GoodsOrderIn goods = goodsOrderInService.selectById(standard.getGoodsId());
                standard.setGoodsName(goods.getName());
                standard.setGoodsModel(goods.getModel());
                standard.setGoodsCode(goods.getCode());
            }
            factoryTestStandardService.insert(standard);
        }
        return AjaxResult.success("新增检验标准完成！");
    }


    /**
     * 修改检验标准
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestStandard:edit')")
    @Log(title = "检验标准", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody FactoryTestStandard entity) {
        return toAjax(factoryTestStandardService.update(entity));
    }

    /**
     * 删除检验标准
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestStandard:remove')")
    @Log(title = "检验标准", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(factoryTestStandardService.deleteByIds(ids));
    }

    /**
     * 获取检验标准详细信息 根据商品id
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestStandard:query')")
    @GetMapping(value = "getTableColumns/{tableName}/{goodsId}")
    public AjaxResult getTableColumnsByGoodsId(@PathVariable("tableName") String tableName, @PathVariable("goodsId") String goodsId) {


        FactoryTestStandard param = new FactoryTestStandard();
        param.setGoodsId(goodsId);
        param.setTestType(tableName);
        List<FactoryTestStandard> standardList =  factoryTestStandardService.selectList(param);
        //判断该产品是否已经存在检验标准 如果存在则更新
        if(!standardList.isEmpty()){
            return AjaxResult.success(standardList);
        }

        GenTable genTable = genTableService.selectGenTableByName(tableName);
        List<GenTableColumn> allList = genTableColumnService.selectGenTableColumnListByTableId(genTable.getTableId());
        List<FactoryTestStandard> list = new ArrayList<>();
        for (GenTableColumn column : allList) {
            if (StringUtils.isNotBlank(column.getJavaField()) && !Arrays.toString(BusinessConstants.standardIgnoreType).contains(column.getJavaField())) {
                FactoryTestStandard factoryTestStandard = new FactoryTestStandard();
                factoryTestStandard.setPropName(column.getJavaField());
                factoryTestStandard.setPropNameCh(column.getColumnComment());
                factoryTestStandard.setResultType("0");
                factoryTestStandard.setPropValueMax("0");
                factoryTestStandard.setPropValueMin("0");
                list.add(factoryTestStandard);
            }
        }
        return AjaxResult.success(list);
    }

    /**
     * 获取检验标准详细信息 根据商品id
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestStandard:query')")
    @GetMapping(value = "getTableColumnsByMaterialId/{tableName}/{materialId}")
    public AjaxResult getTableColumns(@PathVariable("tableName") String tableName, @PathVariable("materialId") String materialId) {
        FactoryTestStandard param = new FactoryTestStandard();
        param.setMaterialId(materialId);
        param.setTestType(tableName);
        List<FactoryTestStandard> standardList =  factoryTestStandardService.selectList(param);
        //判断该产品是否已经存在检验标准 如果存在则更新
        if(!standardList.isEmpty()){
            return AjaxResult.success(standardList);
        }

        GenTable genTable = genTableService.selectGenTableByName(tableName);
        List<GenTableColumn> allList = genTableColumnService.selectGenTableColumnListByTableId(genTable.getTableId());
        List<FactoryTestStandard> list = new ArrayList<>();
        for (GenTableColumn column : allList) {
            if (StringUtils.isNotBlank(column.getJavaField()) && !Arrays.toString(BusinessConstants.standardIgnoreType).contains(column.getJavaField())) {
                FactoryTestStandard factoryTestStandard = new FactoryTestStandard();
                factoryTestStandard.setPropName(column.getJavaField());
                factoryTestStandard.setPropNameCh(column.getColumnComment());
                factoryTestStandard.setResultType("0");
                factoryTestStandard.setPropValueMax("0");
                factoryTestStandard.setPropValueMin("0");
                list.add(factoryTestStandard);
            }
        }
        return AjaxResult.success(list);
    }

}
